package activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.hichip.campro.R;
import base.HiActivity;
import bean.MyCamera;
import butterknife.BindView;
import common.HiDataValue;
import common.TitleView;
import utils.EmojiFilter;
import utils.FullCharUnionFilter;
import utils.HiToast;
import utils.HiTools;
import utils.NotCopyAndPaste;
/**
 * 密码错误时 重新输入密码的界面
 * Created by lt on 2018\2\24 0024.
 */
public class EditCameraActivity extends HiActivity implements View.OnClickListener {
    @BindView(R.id.title) TitleView title;
    @BindView(R.id.et_username) EditText et_username;
    @BindView(R.id.et_password) EditText et_password;
    @BindView(R.id.tv_name_uid) TextView tv_name_uid;
    @BindView(R.id.but_application) Button but_application;
    private MyCamera mMyCamera;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_edit_camera;
    }

    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        getIntentData();
        if(HiTools.cameraWhetherNull(mMyCamera,this)) return;
        initViewAndData();
        setListeners();
    }

    private void setListeners() {
        but_application.setOnClickListener(this);
    }

    private void getIntentData() {
       String uid=getIntent().getStringExtra(HiDataValue.EXTRAS_KEY_UID);
       if(TextUtils.isEmpty(uid)) finish();
       for(MyCamera camera:HiDataValue.CameraList){
           if(uid.equalsIgnoreCase(camera.getUid())){
               this.mMyCamera=camera;
               break;
           }
       }
    }

    private void initViewAndData() {
        title.setTitle(getString(R.string.input_correct));
        title.setButton(TitleView.NAVIGATION_BUTTON_LEFT);
        title.setNavigationBarButtonListener(new TitleView.NavigationBarButtonListener() {
            @Override
            public void OnNavigationButtonClick(int which) {
                switch (which){
                    case TitleView.NAVIGATION_BUTTON_LEFT:
                        EditCameraActivity.this.finish();
                        break;
                }
            }
        });
        et_username.setCustomSelectionActionModeCallback(new NotCopyAndPaste());
        et_password.setCustomSelectionActionModeCallback(new NotCopyAndPaste());
        et_username.setFilters(new InputFilter[]{new InputFilter.LengthFilter(63),new EmojiFilter(),new FullCharUnionFilter(EditCameraActivity.this)});
        et_password.setFilters(new InputFilter[]{new InputFilter.LengthFilter(63),new EmojiFilter(),new FullCharUnionFilter(EditCameraActivity.this)});
        tv_name_uid.setText(mMyCamera.getNikeName()+" ("+mMyCamera.getUid()+")");

        et_username.setText(mMyCamera.getUsername());
        et_password.setText(mMyCamera.getPassword());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.but_application:
                chickDone();
                break;
        }
    }

    private void chickDone() {
        String str_username=et_username.getText().toString();
        String str_password=et_password.getText().toString();

        if(str_username.length()== 0 ) {
            showAlert(getText(R.string.tips_null_username));
            return;
        }

        if((!TextUtils.isEmpty(str_username)&&str_password.getBytes().length>63)||(!TextUtils.isEmpty(str_password)&&str_password.getBytes().length>63)){
            HiToast.showToast(this, getString(R.string.toast_user_pass_tolong));
            return;
        }
        mMyCamera.setPassword(str_password);
        mMyCamera.setUsername(str_username);
        mMyCamera.disconnect();
        mMyCamera.connect();
        mMyCamera.updateInDatabase(this);

        Intent intent = new Intent();
        intent.setAction(HiDataValue.ACTION_CAMERA_INIT_END);
        sendBroadcast(intent);
        this.finish();
    }
}
